<?php if (isset($breadcrumb)) { ?>
<div class="breadcrumb">
    <?php foreach ($breadcrumb as $item) { ?>
    <a href="<?php echo $item['href']; ?>"><?php echo $item['text']; ?></a>
    <?php if ($item['last'] == false) { ?>
        ::
        <?php } ?>
    <?php } ?>
</div>
<? } ?>
<? if (isset($message)) { ?>
<div class="success"><?=$message?></div>
<? } ?>
<div class="box">
<div class="heading">
    <h1><img src="<?=$theme_path?>images/admin/category.png" alt=""><?=$title?></h1>

    <div class="buttons">
        <? foreach ($actions as $item) { ?>
        <a onclick="select_all_right_list();$('#form').attr('action','<?=$item['href']?>');$('#form').submit();"
           class="button"><?=$item['text']?></a>
        <? } ?>
    </div>
</div>
<div class="content">
    <form method="post" enctype="multipart/form-data" id="form">
        <table class="form">
            <tbody>
            <?php foreach ($fields as $item) { ?>
            <tr id="row_<?=$item['name']?>" <?=(isset($item['type']) && $item['type'] == 'hidden') ? 'style="display:none"' : ''?>>
                <td><?=$item['text']?></td>
                <td>
                    <? if ($item['type'] == 'date') { ?>
                    <input type="text" class="inputdate" id="<?=$item['name']?>"
                           value="<?= date('m/d/Y',$item['value'])?>"/>
                    Giờ :
                    <select id="<?=$item['name']?>-hour">
                        <?php for ($hour = 0; $hour < 24; $hour++): ?>
                        <?php if($hour < 10) $hour = '0' . $hour ?>
                        <option value="<?php echo $hour ?>"><?php echo $hour ?></option>
                        <?php endfor; ?>
                    </select>
                    Phút :
                    <select id="<?=$item['name']?>-minute">
                        <?php for ($minute = 0; $minute < 60; $minute++): ?>
                        <?php if($minute < 10) $minute = '0' . $minute ?>
                        <option value="<?php echo $minute ?>"><?php echo $minute ?></option>
                        <?php endfor; ?>
                    </select>
                        <input type="hidden" name="<?=$item['name']?>" id="<?=$item['name']?>-total-date"
                           value="<?php echo date('m/d/Y H:i:s',$item['value'])?>" />
                    <script type="text/javascript">

                        $(document).ready(function () {
                            <!--            SET DATEPICKER TO DATE TEXT FIELD-->
                            var dateInput = $('#<?=$item['name']?>');
                            var hourInput = $('#<?=$item['name']?>-hour');
                            var minuteInput = $('#<?=$item['name']?>-minute');
                            var totalDate = $('#<?=$item['name']?>-total-date');
                            hourInput.val('<?php echo date('H',$item['value']) ?>');
                            minuteInput.val('<?php echo date('i',$item['value']) ?>');
                            dateInput.DatePicker({
                                format:'m/d/Y',
                                date:dateInput.val(),
                                current:dateInput.val(),
                                starts:1,
                                position:'r',
                                onBeforeShow:function () {
                                    dateInput.DatePickerSetDate(dateInput.val(), true);
                                },
                                onChange:function (formated, dates) {
                                    dateInput.val(formated);
                                    fill_date_<?php echo $item['name']?>();
                                }
                            });

                            function fill_date_<?php echo $item['name']?>()
                            {
                                totalDate.val(dateInput.val() + ' ' + hourInput.val() + ':' + minuteInput.val() + ':00');
                            }

                            hourInput.change(function(){
                                fill_date_<?php echo $item['name']?>();
                            });

                            minuteInput.change(function(){
                                fill_date_<?php echo $item['name']?>();
                            });
                        });
                    </script>
                    <? } elseif ($item['type'] == 'text') { ?>
                    <input type="text" name="<?=$item['name']?>" size="100" value="<?=$item['value']?>"/>
                    <? } else if ($item['type'] == 'label') { ?>
                    <?= $item['value'] ?>
                    <? } else if ($item['type'] == 'hidden') { ?>
                    <input type="hidden" name="<?=$item['name']?>" value="<?=$item['value']?>"/>
                    <? } else if ($item['type'] == 'textarea') { ?>
                    <textarea name="<?=$item['name']?>" rows="10" cols="150"><?=$item['value']?></textarea>
                    <? } else if ($item['type'] == 'checkbox') { ?>
                    <input type="checkbox" name="<?=$item['name']?>"/>
                    <? } else if ($item['type'] == 'list') { ?>
                    <table class="list_select">
                        <tr>
                            <td>
                                <select class='left-list' multiple="multiple">
                                    <? foreach ($item['options'] as $option) { ?>
                                    <option
                                        value="<?=$option->{$item['select_value']}?>" <?=$option->{$item['select_value']} == $item['value'] ? 'selected=""' : '' ; ?>><?=$option->{$item['select_display']}?></option>
                                    <? } ?>
                                </select>
                            </td>
                            <td style="text-align: center">
                                <a onclick="pass_right_all_<?=$item['name']?>()" href="javascript:void('');"
                                   class="button">>></a></br></br>
                                <a onclick="pass_right_<?=$item['name']?>()" href="javascript:void('');" class="button">></a></br></br>
                                <a onclick="pass_left_<?=$item['name']?>()" href="javascript:void('');"
                                   class="button"><</a></br></br>
                                <a onclick="pass_left_all_<?=$item['name']?>()" href="javascript:void('');"
                                   class="button"><<</a>
                            </td>
                            <td>
                                <select class="right-list" name='<?=$item['name']?>[]' multiple="multiple">
                                </select>
                            </td>
                        </tr>
                    </table>
                    <script type="text/javascript">

                        $(document).ready(function () {
                            pass_exist_right_<?=$item['name']?>();
                        });

                        left = $('#row_<?=$item['name']?> .left-list');
                        right = $('#row_<?=$item['name']?> .right-list');

                        function pass_exist_right_<?=$item['name']?>() {
                            <?foreach ($item['options_right'] as $child) { ?>
                                right.append(left.find('option:[value=<?=$child->$item['name']?>]'));
                                <? }?>
                        }

                        function pass_right_<?=$item['name']?>() {
                            right.append(left.find('option:selected'));
                        }

                        function pass_right_all_<?=$item['name']?>() {
                            left.children('option').each(function () {
                                right.append($(this));
                            });
                        }

                        function pass_left_all_<?=$item['name']?>() {
                            right.children('option').each(function () {
                                left.append($(this));
                            });
                        }

                        function pass_left_<?=$item['name']?>() {
                            left.append(right.find('option:selected'));
                        }
                    </script>
                    <? } else if ($item['type'] == 'select') { ?>
                    <select name='<?=$item['name']?>'>
                        <option value=""> ---</option>
                        <? foreach ($item['options'] as $option) { ?>
                        <option
                            value="<?=$option->{$item['select_value']}?>" <?=$option->{$item['select_value']} == $item['value'] ? 'selected=""' : '' ; ?>><?=$option->{$item['select_display']}?></option>
                        <? } ?>
                    </select>
                    <? } else if ($item['type'] == 'image') { ?>
                    <img src="<?=base_url() . $item['value']?>" style="max-width:300px; max-height:200px"/><br/>
                    <? } else if ($item['type'] == 'file') { ?>
                    <input type="file" name="<?=$item['name']?>" value="<?=$item['value']?>">
                    <? } else if ($item['type'] == 'add_file') { ?>
                    <input type="button" onclick="add_new_file()" value="<?=$item['text']?>">

                    <script type="text/javascript">

                        function validateForm() {
                            var x = document.forms["form"]["full_name"].value;
                            if (x == null) {
                                alert("First name must be filled out");
                                return false;
                            }
                        }

                        $file_id = 0;
                        function add_new_file() {
                            $file_id++;
                            var tr = $('<tr>');
                            tr.append('<td>');
                            var td = $('<td>');
                            var new_file = $('<input type="file">');
                            new_file.attr('name', "<?=$item['name']?>" + $file_id);
                            td.append(new_file);
                            var button = $('<input type="button" value="Xóa hình này">');
                            button.click(function () {
                                tr.remove();
                            });
                            td.append(button);
                            tr.append(td);
                            $('.form').append(tr);
                        }
                    </script>
                    <?
                } else if ($item['type'] == 'images') {
                    if (!$item['value'])
                        echo "Chưa có hình";
                    else {
                        foreach ($item['value'] as $item) {
                            ?>
                            <div class="image-cover" id="item_<?=$item->id?>"><img src="<?=$item->thumb_image?>"/>

                                <div class="action"><a class="delete" href="javascript:;"
                                                       onclick="javascript:delImg('<?=$item->image_id?>') ">Xóa</a>
                                </div>
                                <input type="hidden" name="image_del_id[]" value="<?=$item->image_id?>"/>
                                <input type="hidden" id="image_del_<?=$item->image_id?>" name="image_del[]"
                                       value="false"/>
                            </div>

                            <?
                        }
                    }
                } ?>

                </td>
            </tr>
                <?php } ?>

            </tbody>
        </table>
    </form>
    <div class="buttons">
        <? foreach ($actions as $item) { ?>
        <a onclick="select_all_right_list();$('#form').attr('action','<?=$item['href']?>');$('#form').submit();"
           class="button"><?=$item['text']?></a>
        <? } ?>
    </div>
</div>
</div>
<script type="text/javascript">
    function select_all_right_list() {
        $('.right-list').children('option').each(function () {
            $(this).attr('selected', true);
        });
    }
</script>